package com.zlkj.ssm.shop.web.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.zlkj.ssm.shop.core.cache.FrontCache;
import com.zlkj.ssm.shop.core.cache.ManageCache;

/**
 * 系统配置加载监听器
 */
@WebListener
public class SystemListener implements ServletContextListener {
	private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SystemListener.class);

	public void contextDestroyed(ServletContextEvent arg0) {
	}

	public void contextInitialized(ServletContextEvent arg0) {
		try {
			WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext());
			FrontCache frontCache = (FrontCache) context.getBean("frontCache");
			ManageCache manageCache = (ManageCache) context.getBean("manageCache");
			frontCache.loadAllCache();// 前台缓存加载
			manageCache.loadAllCache();// 后台缓存加载
		} catch (Throwable e) {
			e.printStackTrace();
			logger.error("System load faild!" + e.getMessage());
			try {
				throw new Exception("系统初始化失败！");
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
	}

}
